package com.example.moshudriver.adapter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.BeeFramework.adapter.BeeBaseAdapter;
import com.sgr.dykdriver.R;
import com.example.moshudriver.protocol.USER;
import com.nostra13.universalimageloader.core.ImageLoader;

public class VipListAdapter extends BeeBaseAdapter {
	protected ImageLoader mImageLoader = ImageLoader.getInstance();
	Context cs;

	public VipListAdapter(Context c, ArrayList orderList) {
		// cs=c;
		super(c, orderList);
	}

	public class E0_OrderCellHolder extends BeeCellHolder {
		private TextView vip_name;
		private TextView vip_phone;
		private TextView vip_role;
		private TextView vip_is_online;
		private ImageView iv_driver_phone;
	}

	@Override
	protected BeeCellHolder createCellHolder(View cellView) {
		E0_OrderCellHolder holder = new E0_OrderCellHolder();

		holder.vip_name = (TextView) cellView.findViewById(R.id.vip_name);
		holder.vip_phone = (TextView) cellView.findViewById(R.id.vip_mobile);
		holder.vip_role = (TextView) cellView.findViewById(R.id.vip_role);
		holder.iv_driver_phone = (ImageView) cellView
				.findViewById(R.id.iv_driver_phone);
		holder.vip_is_online = (TextView) cellView
				.findViewById(R.id.vip_is_online);

		return holder;
	}

	// 绑定订单数据
	@Override
	protected View bindData(int position, View cellView, ViewGroup parent,
			BeeCellHolder h) {
		final USER coupon = (USER) dataList.get(position);
		final E0_OrderCellHolder holder = (E0_OrderCellHolder) h;
		// System.out.println("名字："+coupon.name.name);
		// System.out.println("手机："+coupon.dmobile.mobile);
		// System.out.println("角色："+coupon.drole.role);
		System.out.println("角色：" + coupon.user);

		try {
			JSONObject obj = new JSONObject(coupon.user);
			int on_off = obj.getInt("on_off");
			int role = obj.getInt("role");
			String name = obj.getString("name");
			String mobile = obj.getString("mobile");
			
			if (coupon != null) {
				holder.vip_name.setText(name);
				holder.vip_phone.setText(mobile);
				if (role == 2) {
					holder.vip_role.setText("司机");
				} else {
					holder.vip_role.setText("乘客");
				}

				if (on_off == 0) {
					holder.vip_is_online.setText("离线");
				} else {
					holder.vip_is_online.setText("在线");
				}
				holder.iv_driver_phone.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(Intent.ACTION_CALL, Uri
								.parse("tel:" + holder.vip_phone.getText()));
						// startActivity(intent);
						mContext.startActivity(intent);
					}
				});

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		

		return null;
	}

	@Override
	public View createCellView() {
		return mInflater.inflate(R.layout.vip_cell, null);
	}
}
